<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/

class mmg_api{

	var $version="0.0.1";

	public function __construct() {
		$this->pz = &get_instance();
	}

	public function install($md){
		$sqlfile = APPPATH.'modules/'.$md.'/install.sql';
		$result = $this->exesql($sqlfile);
		if($result==true){
			$mdapi = load_api($md);
				
			$api = load_api('setting');
			$settingkey = 'modules';
			$setting = $api->getSetting($settingkey);
			$mdarr['name'] = $md;
			$mdarr['version'] = $mdapi->version;
			$mdarr['console'] = $mdapi->console;
			$mdarr['title'] = lang('title',$md);
			$setting[$md] = $mdarr;
			$api->updateSetting($settingkey,$setting);
			$this->registerEvent($mdapi, $md);
			if(method_exists($mdapi, 'afterinstall')){
				return $mdapi->afterinstall();
			}else{
				return true;
			}
		}else{
			return $result;
		}
	}

	public function invokeEvent($event,$args){
		$api = load_api('setting');
		$settingkey = 'registeredEvents';
		$setting = $api->getSetting($settingkey);
		if(isset($setting[$event])){
			$plugins = $setting[$event];
			foreach ($plugins as $md){
				$mdapi = load_api($md);
				if(method_exists($mdapi, $event)){
					return $mdapi->$event($args);
				}else{
					return true;
				}
			}
		}
	}

	public function registerEvent($mdapi,$md){
		if($mdapi->registeredEvents){
			$api = load_api('setting');
			$settingkey = 'registeredEvents';
			$setting = $api->getSetting($settingkey);
			foreach ($mdapi->registeredEvents as $event){
				$setting[$event] = (isset($setting[$event]))?$setting[$event]:array();
				if(!in_array($md, $setting[$event])){
					$setting[$event][] = $md;
				}
			}
			$api->updateSetting($settingkey,$setting);
		}
	}


	public function unregisterEvent($mdapi,$md){
		if($mdapi->registeredEvents){
			$api = load_api('setting');
			$settingkey = 'registeredEvents';
			$setting = $api->getSetting($settingkey);
			foreach ($mdapi->registeredEvents as $event){
				if(isset($setting[$event])&&in_array($md, $setting[$event])){
					$key = array_key($setting[$event], $md);
					if($key!=-1){
						array_remove($setting[$event], $key);
					}
				}
			}
			$api->updateSetting($settingkey,$setting);
		}
	}

	public function uninstall($md){
		$sqlfile = APPPATH.'modules/'.$md.'/uninstall.sql';
		$result = $this->exesql($sqlfile);
		if($result==true){
			$mdapi = load_api($md);
				
			$api = load_api('setting');
			$settingkey = 'modules';
			$setting = $api->getSetting($settingkey);
			unset($setting[$md]);
			$api->updateSetting($settingkey,$setting);
			$this->unregisterEvent($mdapi, $md);
			if(method_exists($mdapi, 'afteruninstall')){
				return $mdapi->afteruninstall();
			}else{
				return true;
			}
		}else{
			return $result;
		}
	}

	private function exesql($sqlfile){
		if(file_exists($sqlfile)){
			$sql = explode(";",file_get_contents($sqlfile));
			$db = load_db('mmg');
			foreach($sql as $query){
				if(trim($query) == '') continue;
				$query = str_replace('{dbpre}', $this->dbpre, $query);
				$result = $db->runSql($query);
				if (!$result) {
					$message  = 'Invalid query: ' . $query . "\n<br />";
					$message .= 'Whole query: ' . $query;
					return $message;
				}
			}
		}
		return true;
	}

}